<template>
  <div class="login-group">

    <div class="login-info">
      <h2>后台管理登录</h2>
      <el-form :model="loginInfo" :rules="rules" ref="loginForm" label-position="right" label-width="80px"
               @keydown.enter.native="login('loginForm')">

        <el-form-item prop="username" label-width="90px">
          <span class="span-info" slot="label">用户名</span>
          <el-input v-model="loginInfo.username" prefix-icon="el-icon-user" placeholder="请输入用户名"></el-input>
        </el-form-item>
        <el-form-item prop="password" label-width="90px">
          <span class="span-info" slot="label">密&nbsp;&nbsp;&nbsp;&nbsp;码</span>
          <el-input v-model="loginInfo.password" prefix-icon="el-icon-lock" show-password placeholder="请输入密码"></el-input>
        </el-form-item>
        <el-form-item label-width="90px">
          <span slot="label">
            <img :src="captchaSrc" @click="getCaptcha" style="height: 40px;width: 80px" />
          </span>
          <el-input label-width="200px" :span="12" v-model="loginInfo.captcha" prefix-icon="el-icon-s-promotion" placeholder="请输入验证码"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button @click="login('loginForm')" type="primary">登录</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { uuid } from 'vue-uuid'

const loginInfo = {
  username: '',
  password: '',
  captcha: '',
  uuid: ''
}
export default {
  name: 'Login',
  data () {
    return {
      loginInfo: {
        ...loginInfo
      },
      captchaSrc: '',
      rules: {
        username: [
          {
            required: true,
            message: '用户名不为空',
            trigger: 'blur'
          },
          {
            min: 5,
            max: 15,
            message: '长度在5到15之间',
            trigger: 'blur'
          }
        ],
        password: [
          {
            required: true,
            message: '密码不为空',
            trigger: 'blur'
          },
          {
            min: 5,
            max: 15,
            message: '长度在 5 到 15 个字符',
            trigger: 'blur'
          }
        ],
        captcha: [
          {
            required: true,
            message: '验证码不为空',
            trigger: 'blur'
          },
          {
            min: 1,
            max: 4,
            message: '长度在 1 到 4 个字符',
            trigger: 'blur'
          }

        ]
      },
      loading: false
    }
  },
  methods: {
    getCaptcha () {
      this.loginInfo.uuid = uuid.v4()
      this.captchaSrc = `${process.env.VUE_APP_BASE_API}/captcha.jpg?uuid=${this.loginInfo.uuid}`
    },
    login (ref) {
      this.$refs[ref].validate(vaild => {
        if (vaild) {
          /* 开启遮罩层 */
          this.loading = this.$loading({ text: '加载中……' })
          this.$http.post('/sys/login', this.loginInfo).then(({ data }) => {
            console.log(data)
            if (data.code === 200) {
              // 获取token存入cookie
              this.$cookies.set('token', data.token)
              /* 提示信息，页面跳转 */
              this.$message({
                type: 'success',
                message: '登录成功',
                onClose: () => {
                  this.$router.replace('/')
                }
              })
            } else {
              this.getCaptcha()
              this.$alert(data.msg, { type: 'error' })
            }
          }).catch(err => {
            console.error(err)
            this.$alert('与服务器连接失败，请稍后再试……', { type: 'error' })
          }).finally(() => {
            this.loading.close()
          })
        }
      }
      )
    }

  },
  created () {
    this.$cookies.remove('token')
    this.getCaptcha()
  }
}
</script>

<style scoped lang="scss">
.login-group {
  height: 100vh;
  width: 100vw;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  background: url("../assets/images/login-bg.jpg") no-repeat center/cover;

  h2 {
    margin-bottom: 20px;
    color: orange;
  }

  .login-info {
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    height: 40vh;
    width: 33vw;
    border-radius: 10px;
    background-color: rgba(255, 255, 255, .3);
    padding: 40px 20px;

    .span-info {
      color: orange;
    }
  }
}
</style>
